home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / copy / copy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-13  |  1.9 KB  |  96 lines

  1. /* Simple file copy program.  */
  2.  
  3. #ifndef lint
  4. static char rcsid[] = "$Header: /r3/kupfer/spriteserver/tests/copy/RCS/copy.c,v 1.2 91/12/12 22:33:09 kupfer Exp $";
  5. #endif
  6.  
  7. #include <sprite.h>
  8. #include <mach.h>
  9. #include <mach_error.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/fcntl.h>
  13. #include <test.h>
  14. #include <unistd.h>
  15.  
  16. /* #define USE_MALLOC    1 */
  17.  
  18. static void CopyFile();
  19.  
  20. int
  21. main(argc, argv)
  22.     int argc;
  23.     char *argv[];
  24. {
  25.     if (argc != 3) {
  26.     Test_PutMessage("usage: copy from to\n");
  27.     exit(1);
  28.     }
  29.     CopyFile(argv[1], argv[2]);
  30.     
  31.     return 0;
  32. }
  33.  
  34. static void
  35. CopyFile(fromFileName, toFileName)
  36.     char *fromFileName;
  37.     char *toFileName;
  38. {
  39.     int fromFd;
  40.     int toFd;
  41.     Address buffer;
  42.     int nChars;
  43. #ifndef USE_MALLOC
  44.     kern_return_t kernStatus;
  45. #endif
  46.     int bufferSize;        /* number of bytes in buffer */
  47.  
  48.     fromFd = open(fromFileName, O_RDONLY);
  49.     if (fromFd < 0) {
  50.     perror(fromFileName);
  51.     exit(1);
  52.     }
  53.     toFd = open(toFileName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  54.     if (toFd < 0) {
  55.     perror(toFileName);
  56.     exit(1);
  57.     }
  58. #ifdef USE_MALLOC
  59.     bufferSize = 2 * vm_page_size;
  60.     buffer = malloc(bufferSize);
  61.     if (buffer == NULL) {
  62.     Test_PutMessage("Couldn't malloc buffer\n");
  63.     exit(1);
  64.     } else {
  65.     Test_PutMessage("buffer at ");
  66.     Test_PutHex(buffer);
  67.     Test_PutMessage("\n");
  68.     }
  69. #else
  70.     bufferSize = 2 * vm_page_size;
  71.     buffer = 0;
  72.     kernStatus = vm_allocate(mach_task_self(), (vm_address_t *)&buffer, 
  73.                  bufferSize, TRUE);
  74.     if (kernStatus != KERN_SUCCESS) {
  75.     Test_PutMessage("Couldn't allocate buffer: ");
  76.     Test_PutDecimal(kernStatus);
  77.     Test_PutMessage("\n");
  78.     exit(1);
  79.     }
  80. #endif
  81.  
  82.     while ((nChars = read(fromFd, buffer, bufferSize)) > 0) {
  83.     Test_PutMessage("Read ");
  84.     Test_PutDecimal(nChars);
  85.     Test_PutMessage("\n");
  86.     if (write(toFd, buffer, nChars) != nChars) {
  87.         Test_PutMessage("short write.\n");
  88.         exit(1);
  89.     }
  90.     }
  91.     if (nChars < 0) {
  92.     perror("read");
  93.     exit(1);
  94.     }
  95. }
  96.